home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 18.3 KB | 520 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWStrs.cpp
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #include <Types.h>
- #endif
-
- #ifndef FWEXCLIB_H
- #include "FWExcLib.h"
- #endif
-
- #ifndef FWMATH_H
- #include "FWMath.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWSTRTOO_H
- #include "FWStrToo.h"
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment Strings
- #endif
-
- //========================================================================================
- // CLASS FW_CString
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CString::~FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::~FW_CString()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString (Default constructor)
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString() :
- fRepresentation(0),
- fLength(0),
- #ifdef FW_VARIABLE_WIDTH_CHARACTERS
- fByteLength(0),
- #endif
- fCapacity(0)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString (Copy constructor)
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(const FW_CString &string) :
- fRepresentation(0),
- fLength(0), // Derived class is responsible for updating cached lengths
- #ifdef FW_VARIABLE_WIDTH_CHARACTERS
- fByteLength(0),
- #endif
- fCapacity(0)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::Retrieve
- //----------------------------------------------------------------------------------------
-
- void FW_CString::Retrieve(FW_Char* items, FW_CharacterCount numberItems, FW_CharacterPosition position) const
- {
- const FW_Byte* rep = fRepresentation;
- FW_ASSERT(numberItems >= 0);
- FW_ASSERT(position>=0);
- FW_ASSERT(position+numberItems <= fLength+1); // allow retrieval of NUL terminator
- const FW_Byte* start = rep + FW_BytesInString(rep, position);
- FW_ByteCount bytes = FW_BytesInString(start, numberItems);
- FW_BlockMove(start, (FW_Byte*) items, bytes);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::Insert
- //----------------------------------------------------------------------------------------
-
- void FW_CString::Insert(const FW_Char* items,
- FW_CharacterCount numberItems,
- FW_CharacterPosition position)
- {
- FW_ASSERT(numberItems >= 0);
- FW_ASSERT(position>=0 && position<=fLength);
- if (numberItems > 0)
- {
- FW_ByteCount bytesInItems = FW_BytesInString(items, numberItems);
- FW_ByteCount capacityNeeded = GetByteLength()+bytesInItems;
- GrowCapacity(capacityNeeded);
- FW_ASSERT(fCapacity >= capacityNeeded);
-
- FW_ByteCount bytesToPosition = FW_BytesInString(fRepresentation, position);
- FW_Byte* blockToMove = fRepresentation + bytesToPosition;
- FW_BlockMove(blockToMove,
- blockToMove+bytesInItems,
- GetByteLength()-bytesToPosition+sizeof(FW_Char));
- FW_BlockMove((const FW_Byte*) items, blockToMove, bytesInItems);
- SetLength(fLength + numberItems, capacityNeeded);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::Delete
- //----------------------------------------------------------------------------------------
-
- void FW_CString::Delete(FW_CharacterCount numberItems, FW_CharacterPosition position)
- {
- FW_ASSERT(numberItems >= 0);
- FW_ASSERT(position>=0 && position<=fLength);
- FW_ASSERT(position+numberItems <= fLength);
- if (numberItems > 0)
- {
- FW_Byte* atPosition = FW_BytePositionInString(fRepresentation, position);
- FW_ByteCount toDelete = FW_BytesInString(atPosition, numberItems);
- FW_ByteCount toMove = FW_BytesInString(atPosition+toDelete, fLength-(position+numberItems));
- FW_BlockMove(atPosition+toDelete, atPosition, toMove);
- SetLength(fLength-numberItems, GetByteLength() - toDelete);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::Replace
- //----------------------------------------------------------------------------------------
-
- void FW_CString::Replace(const FW_Char* items, FW_CharacterCount numberItems, FW_CharacterPosition position)
- {
- FW_ASSERT(numberItems >= 0);
- FW_ASSERT(position>=0 && position<=fLength);
- if (numberItems > 0)
- {
- FW_ByteCount bytesToPosition = FW_BytesInString(fRepresentation, position);
- FW_ByteCount bytesInItems = FW_BytesInString(items, numberItems);
- FW_CharacterCount charsToReplace = FW_Minimum(numberItems, fLength-position);
- FW_ByteCount bytesToReplace = FW_BytesInString(fRepresentation+bytesToPosition, charsToReplace);
-
- FW_ByteCount capacityNeeded = GetByteLength() - bytesToReplace + bytesInItems;
- FW_ByteCount capacity = GrowCapacity(capacityNeeded);
- FW_ASSERT(capacityNeeded <= capacity);
-
- FW_ByteCount bytesAtEndToMove = GetByteLength() - bytesToPosition - bytesToReplace;
- if (bytesAtEndToMove>0 && bytesToReplace!=bytesInItems)
- {
- FW_BlockMove(fRepresentation+bytesToPosition+bytesToReplace,
- fRepresentation+bytesToPosition+bytesInItems,
- bytesAtEndToMove);
- }
-
- FW_BlockMove((const FW_Byte*) items, fRepresentation+bytesToPosition, bytesInItems);
- SetLength(fLength - charsToReplace + numberItems, capacityNeeded);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ToUpper
- //----------------------------------------------------------------------------------------
-
- void FW_CString::ToUpper()
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- tool->ToUpper(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ToLower
- //----------------------------------------------------------------------------------------
-
- void FW_CString::ToLower()
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- tool->ToLower(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::Substitute
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::Substitute(const FW_CString &searchString,
- const FW_CString &substitutionString)
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- return tool->Substitute(*this, searchString, substitutionString);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FindSubString
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::FindSubString(const FW_CString &subString,
- FW_CharacterPosition &foundPosition,
- FW_CharacterPosition startPosition) const
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- return tool->FindSubString(*this, subString, foundPosition, startPosition);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FindCharacter
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::FindCharacter(FW_Char character,
- FW_CharacterPosition &foundPosition,
- FW_CharacterPosition startPosition) const
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- return tool->FindCharacter(*this, character, foundPosition, startPosition);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FindWhiteSpace
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::FindWhiteSpace(FW_CharacterPosition &foundPosition,
- FW_CharacterPosition startPosition) const
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- return tool->FindWhiteSpace(*this, foundPosition, startPosition);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FindNonWhiteSpace
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::FindNonWhiteSpace(FW_CharacterPosition &foundPosition,
- FW_CharacterPosition startPosition) const
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- return tool->FindNonWhiteSpace(*this, foundPosition, startPosition);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::Compare
- //----------------------------------------------------------------------------------------
-
- int FW_CString::Compare(const FW_CString &string) const
- {
- FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
- return tool->CompareStrings(*this, string);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::operator==(const FW_CString &string) const
- {
- return (GetLength()==string.GetLength()) && (Compare(string) == kStringsEqual);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator!=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::operator!=(const FW_CString &string) const
- {
- return (GetLength()!=string.GetLength()) || (Compare(string) != kStringsEqual);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator<
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::operator<(const FW_CString &string) const
- {
- return Compare(string) == kStringOneLess;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator>
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::operator>(const FW_CString &string) const
- {
- return Compare(string) == kStringOneGreater;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator<=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::operator<=(const FW_CString &string) const
- {
- return Compare(string) != kStringOneGreater;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator>=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CString::operator>=(const FW_CString &string) const
- {
- return Compare(string) != kStringOneLess;
- }
-
- //========================================================================================
- // CLASS FW_CDynamicString
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::~FW_CDynamicString
- //----------------------------------------------------------------------------------------
-
- FW_CDynamicString::~FW_CDynamicString()
- {
- FW_START_DESTRUCTOR
- delete [] fRepresentation;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::FW_CDynamicString
- //----------------------------------------------------------------------------------------
-
- FW_CDynamicString::FW_CDynamicString(const FW_CDynamicString &string) :
- FW_CString()
- {
- FW_ByteCount numberBytes = string.GetByteLength();
- AllocateRepresentation(numberBytes);
- Append(string);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::FW_CDynamicString
- //----------------------------------------------------------------------------------------
-
- FW_CDynamicString::FW_CDynamicString(const FW_CString &string) :
- FW_CString()
- {
- FW_ByteCount numberBytes = string.GetByteLength();
- AllocateRepresentation(numberBytes);
- Append(string);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::FW_CDynamicString
- //----------------------------------------------------------------------------------------
-
- FW_CDynamicString::FW_CDynamicString(const FW_Char *items, FW_CharacterCount numberItems) :
- FW_CString()
- {
- FW_ByteCount numberBytes = FW_BytesInString(items, numberItems);
- AllocateRepresentation(numberBytes);
- Append(items, numberItems);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::FW_CDynamicString
- //----------------------------------------------------------------------------------------
-
- FW_CDynamicString::FW_CDynamicString(const FW_Char *items)
- {
- FW_CharacterCount numberItems = FW_StringLength(items);
- FW_ByteCount numberBytes = FW_BytesInString(items, numberItems);
- AllocateRepresentation(numberBytes);
- Append(items, numberItems);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::FW_CDynamicString
- //----------------------------------------------------------------------------------------
-
- FW_CDynamicString::FW_CDynamicString(FW_ByteCount capacity) :
- FW_CString()
- {
- AllocateRepresentation(capacity);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::GrowCapacity
- //----------------------------------------------------------------------------------------
-
- FW_ByteCount FW_CDynamicString::GrowCapacity(FW_ByteCount capacityNeeded)
- {
- Resize(capacityNeeded);
- return fCapacity;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CDynamicString::Resize
- //----------------------------------------------------------------------------------------
-
- void FW_CDynamicString::Resize(FW_ByteCount newCapacity)
- {
- if (fCapacity < newCapacity)
- {
- FW_Byte *newRep = new FW_Byte[newCapacity+sizeof(FW_Char)];
- FW_BlockMove(fRepresentation, newRep, fCapacity+sizeof(FW_Char));
- delete [] fRepresentation;
- fRepresentation = newRep;
- fCapacity = newCapacity;
- }
- }
-
- //========================================================================================
- // CLASS FW_CStringReader
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CStringReader::FW_CStringReader
- //----------------------------------------------------------------------------------------
-
- FW_CStringReader::FW_CStringReader(
- const FW_CString& string) :
- FW_CTextReader(string.fRepresentation,
- string.fRepresentation+string.GetByteLength(),
- string.GetLength())
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CStringReader::FW_CStringReader
- //----------------------------------------------------------------------------------------
-
- FW_CStringReader::FW_CStringReader(
- const FW_CString &string,
- FW_CharacterPosition start,
- FW_CharacterCount length) :
- FW_CTextReader(FW_BytePositionInString(string.fRepresentation, start),
- FW_BytePositionInString(string.fRepresentation, start+length),
- length)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CStringReader::DoGetNextBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CStringReader::DoGetNextBuffer()
- {
- FW_ASSERT(FALSE); // one contigous block, there is no next buffer
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CStringReader::DoGetPreviousBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CStringReader::DoGetPreviousBuffer()
- {
- FW_ASSERT(FALSE); // one contigous block, there is no previous buffer
- }
-
- //========================================================================================
- // CLASS FW_CStringWriter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CStringWriter::FW_CStringWriter
- //----------------------------------------------------------------------------------------
-
- FW_CStringWriter::FW_CStringWriter(
- FW_CString &string,
- FW_TextWriterMode mode,
- unsigned short expansion) :
- FW_CTextWriter(0, 0),
- fString(string),
- fExpansion(expansion)
- {
- fBuffer = new FW_Byte[fExpansion];
- fNext = fBuffer;
- fLimit = fBuffer + fExpansion;
- if (mode == FW_kTextAppend)
- {
- fBufferSum = fString.GetLength();
- }
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CStringWriter::~FW_CStringWriter
- //----------------------------------------------------------------------------------------
-
- FW_CStringWriter::~FW_CStringWriter()
- {
- FW_START_DESTRUCTOR
- FlushAndUpdateText();
- delete [] fBuffer;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CStringWriter::DoFlushAndGetNextBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CStringWriter::DoFlushAndGetNextBuffer()
- {
- fString.Replace((FW_Char*) fBuffer, FW_CharactersInBlock(fBuffer, fExpansion), fBufferSum);
- fNext = fBuffer;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CStringWriter::FlushAndUpdateText
- //----------------------------------------------------------------------------------------
-
- void FW_CStringWriter::FlushAndUpdateText()
- {
- fString.Replace((FW_Char*) fBuffer, FW_CharactersInBlock(fBuffer, fNext-fBuffer), fBufferSum);
- }
-